﻿// See https://aka.ms/new-console-template for more information
using System.IO.MemoryMappedFiles;
using System.IO.Pipes;
using System.Text;

Console.WriteLine("Hello, World!");

const string mapName = "MySharedMemory";
const int bufferSize = 1024; // 共享内存的大小

using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting(mapName))
using (MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor())
{
    byte[] buffer = new byte[bufferSize];
    Thread.Sleep(5000);
    accessor.ReadArray(0, buffer, 0, bufferSize);

    string message = Encoding.UTF8.GetString(buffer);
    Console.WriteLine("Process B received: " + message);

    byte[] buffer2 = new byte[bufferSize];
    accessor.WriteArray(0, buffer2, 0, bufferSize);
    Thread.Sleep(5000);
}
//// 创建命名管道服务器
//using (NamedPipeServerStream serverPipe = new NamedPipeServerStream("MyNamedPipe", PipeDirection.InOut))
//{
//    Console.WriteLine("Waiting for client to connect...");
//    serverPipe.WaitForConnection();

//    using (StreamReader reader = new StreamReader(serverPipe))
//    using (StreamWriter writer = new StreamWriter(serverPipe))
//    {
//        string request = reader.ReadLine();
//        Console.WriteLine($"Received request: {request}");

//        // 执行操作，这里简单返回一个消息
//        string response = "Response from project2: Hello from project2!";
//        writer.WriteLine(response);
//        writer.Flush();
//    }
//}
Console.ReadLine();
return;